package com.yan.edu.controller;


import com.alibaba.excel.util.StringUtils;
import com.yan.commonutils.R;
import com.yan.edu.client.VodClient;
import com.yan.edu.entity.Video;
import com.yan.edu.service.VideoService;
import com.yan.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/edu/video")
@CrossOrigin
public class VideoController {
    @Autowired
    private VodClient vodClient;
    @Autowired
    private VideoService videoService;
    @ApiOperation(value = "添加小结")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody Video video){
        videoService.save(video);
        return R.ok();
    }
    @ApiOperation(value = "修改小结")
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody Video video){
        videoService.updateById(video);

        return R.ok();
    }
    @ApiOperation(value = "删除小结")
    @DeleteMapping ("{id}")
    public R deleteVideo(@PathVariable String id){
//        根据小结id获取视频id
        Video video = videoService.getById(id);
        String courseId = video.getCourseId();
//        视频ID不为空这删除
        if(!StringUtils.isEmpty(courseId)){
            R r = vodClient.removeVideo(courseId);
            if(r.getCode()==20001){
                throw new GuliException(20001,"删除视频失败，熔断器");
            }
        }
//删除小结
        videoService.removeById(id);
        return R.ok();
    }

}

